﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_update_product : System.Web.UI.Page
{
    dulieuDataContext db = new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            LoadNhomCha();
            LoadNhomCon();
            LoadData();

        }
        FCK.Language = "vi";
        var fileBrowser = new CKFinder.FileBrowser();
        fileBrowser.BasePath = "../ckfinder/";
        fileBrowser.SetupCKEditor(FCK);
    }

    protected void LoadNhomCha()
    {
        drdnhomcha.DataSource = from o in db.Cates where o.TypeCateID == 2 && o.Parent == 0 select o;
        drdnhomcha.DataTextField = "Name";
        drdnhomcha.DataValueField = "Cate_ID";
        drdnhomcha.DataBind();
    }
    protected void LoadNhomCon()
    {
        drdnhomcon.DataSource = from o in db.Cates
                                where o.TypeCateID == 2 && o.Parent == Convert.ToInt32(drdnhomcha.SelectedValue)
                                select o;
        drdnhomcon.DataTextField = "Name";
        drdnhomcon.DataValueField = "Cate_ID";
        drdnhomcon.DataBind();
    }
    protected void LoadData()
    {
        var ProductID = Convert.ToDecimal(Session["id_product"]);
        var product = (from o in db.Products where o.ProductID == ProductID select o).SingleOrDefault();
        txttensp.Text = product.Name;
        txtsoluong.Text = product.Quantity.ToString();
        txtgiaonline.Text = product.PriceOnline.ToString();
        txtgiahang.Text = product.Price.ToString();
        txttomtat.Text = product.Descriptions;
        FCK.Text = product.Detail;
        txtkhuyenmai.Text = product.Km;
        txtbaohanh.Text = product.Ensuarance.ToString();
    }

    protected void imbtthem_Click(object sender, ImageClickEventArgs e)
    {
        var productID = Convert.ToDecimal(Session["id_product"]);
        var product = (from o in db.Products where o.ProductID == productID select o).SingleOrDefault();
        if (product != null)
        {
            product.Name = txttensp.Text;
            product.Quantity = Convert.ToInt32(txtsoluong.Text);
            product.PriceOnline = Convert.ToDouble(txtgiaonline.Text);
            product.Price = Convert.ToDouble(txtgiahang.Text);
            product.Descriptions = txttomtat.Text;
            product.Detail = FCK.Text;
            product.Km = txtkhuyenmai.Text;
            product.Ensuarance = Convert.ToInt32(txtbaohanh.Text);
            product.Cate_ID = chk.Checked ? Convert.ToInt32(drdnhomcon.SelectedValue) : product.Cate_ID;
            product.Image = GetFilePic() == "" ? product.Image : GetFilePic();
        }
        db.SubmitChanges();
        lblthongbao.Text = "cập nhật thành công";
    }
    public string GetFilePic()
    {
        var sfile = "";
        if (FileUpload1.HasFile)
        {
            sfile = Guid.NewGuid().ToString().Substring(0, 10) + FileUpload1.PostedFile.FileName.Remove(0, FileUpload1.PostedFile.FileName.LastIndexOf("."));
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") + sfile);
        }
        return sfile;
    }
    protected void drdnhomcha_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadNhomCon();
    }
}